package com.example.springboot01.webcontroller;

import com.example.springboot01.entity.AddressInfo;
import com.example.springboot01.mapper.AddressInfoMapper;
import com.example.springboot01.service.AddressInfoService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


import com.example.springboot01.entity.ProInfo;
import com.example.springboot01.entity.UserInfo;
import com.example.springboot01.service.ProInfoService;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.thymeleaf.util.StringUtils;

import java.util.List;
import java.util.Objects;

@Controller
public class AddressInfoController {
    @Autowired
    private AddressInfoService addressInfoService;
//    @Autowired
//    private ProInfoService proInfoService;

    //    @RequestMapping("qry_user_list")
//    public String qryUserList(HttpServletRequest request, Model model){
//        String field_name = request.getParameter("field_name");
//        String field_value = request.getParameter("qry_content");
//        System.out.println("controller: field_name--" + field_name
//                + "---field_value---" + field_value);
//        if (field_name == "" ||field_name.equals(null)) {
//            field_name = "user_code";
//            field_value = "";
//        }
//
//        //把查询的工作交给service去做。自己只想得到service返回的结果
//        List<UserInfo> user_list = userInfoSerivce.qryUserList(field_name, field_value);
//        System.out.println("controller：数据集数量：" + user_list.size());
//        model.addAttribute("user_list", user_list);
//
//        return "user_list";
//    }
//
//    @RequestMapping("add_user")
//    public String showAddUser(Model model){
//        List<ProInfo> proInfoList = proInfoService.getProInfoList();
//        model.addAttribute("pro_list", proInfoList);
//
//        return "add_user";
//    }
    @RequestMapping("address_page")
    public String showAddressPage(Model model) {
        return "address/address_page";
    }

    @RequestMapping("address_page1")
    public String showAddressPage1(Model model) {
        return "address/address_page1";
    }

    @RequestMapping("add_address")
    public String showAddUser(Model model) {
        return "address/add_address";
    }

    @RequestMapping("show_edit_address")
    public String showMain() {
        return "address/edit_address";
    }

    @RequestMapping("show_edit_address1")
    @ResponseBody // 返回 JSON 格式数据
    public AddressInfo showEditAddress(HttpServletRequest request, Model model) {
        String address_id = request.getParameter("address_id");
        //根据传入的主键让service获取UserInfo类的对象并返回给userInfo。
        AddressInfo addressInfo = addressInfoService.getAddressInfo(address_id);
        System.out.println(addressInfo.toString());
        model.addAttribute("address_info", addressInfo);
        return addressInfo;
    }

//    @RequestMapping("insert_a_user")//不用动态绑定的的方式
//    public String insertAUser(HttpServletRequest request, RedirectAttributes attributes){
//        int flag = 0;
//        UserInfo userInfo = new UserInfo();
//        userInfo.setUser_code(request.getParameter("user_code"));
//        userInfo.setUser_name(request.getParameter("user_name"));
//        userInfo.setUser_pwd("123456");
//        userInfo.setTelephone(request.getParameter("telephone"));
//        userInfo.setUser_pro_id(Integer.parseInt(request.getParameter("user_pro_id")));
//        System.out.println("controller:" + userInfo.toString());
//        flag = userInfoSerivce.insertAUser(userInfo);
//
//        if (flag >0) {
//            attributes.addAttribute("field_name", "user_code");
//            attributes.addAttribute("qry_content","");
//            return "redirect:qry_user_list";
//        }
//        else
//            return "error";
//    }
//    @RequestMapping("insert_a_user_binding")//动态自动绑定的的方式
//    public String insertAUserBinding(UserInfo userInfo, RedirectAttributes attr){
//        int flag = 0;
//        System.out.println("controller:" + userInfo.toString());
//        flag = userInfoSerivce.insertAUser(userInfo);
//
//        if (flag > 0) {
//            attr.addAttribute("field_name", "user_code");
//            attr.addAttribute("qry_content","");
//            return "redirect:qry_user_list";
//        }
//        else
//            return "error";
//    }

    @RequestMapping("insert_a_address_binding")//动态自动绑定的的方式
    public String insertAAddressBinding(AddressInfo addressInfo, HttpServletRequest request) {
        int flag = 0;
        System.out.println("controller:" + addressInfo.toString());

        if (addressInfo.getDefault_address().equals("1")) {
            UserInfo userInfo = (UserInfo) request.getSession().getAttribute("user_info");
            System.out.println("userInfo.getUser_code" + userInfo.getUser_code());
            String userId=userInfo.getUser_code();
            //将默认地址清空
            addressInfoService.resetAllDefaultAddress(userId);
        }
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("user_info");

        System.out.println("userInfo.getUser_code" + userInfo.getUser_code());
        flag = addressInfoService.insertAAddress(addressInfo, userInfo.getUser_code());

        if (flag > 0) {
            return "redirect:address_page";
        } else
            return "error";
    }
//    @RequestMapping("delete_user")//一般都是假删除，通过update完成。很少用delete真删除
//    public String deleteUser(HttpServletRequest request, RedirectAttributes attributes){
//        int flag = 0;
//        String user_code = request.getParameter("user_code");
//        flag = userInfoSerivce.deleteUserInfo(user_code);
//
//        if (flag > 0) {
//            attributes.addAttribute("field_name", "user_code");
//            attributes.addAttribute("qry_content","");
//            return "redirect:qry_user_list";
//        }
//        else
//            return "error";
//    }

    @RequestMapping("delete_address")//一般都是假删除，通过update完成。很少用delete真删除
    @ResponseBody
    public int deleteAddress(HttpServletRequest request, RedirectAttributes attributes) {
        int flag = 0;

        String address_id = request.getParameter("address_id");
        flag = addressInfoService.deleteAddressInfo(address_id);
        System.out.println("flagflagflag" + flag);
        if (flag > 0) {
            return flag;
        } else
            return 1;
    }

    //当删除地址为默认地址时
    @RequestMapping("updateDefaultAddress")
    public String updateDefaultAddress() {
        int flag = 0, maxId = -1;

        maxId = addressInfoService.getMaxAddressId();//寻找最大的且没有删除的id
        flag = addressInfoService.updateDefaultAddress(maxId);
        if (flag > 0) {
            return "redirect:qry_address_page_list";
        } else
            return "error";
    }

    //设置默认地址
    @RequestMapping("SetDefaultAddress")
    public String SetDefaultAddress(HttpServletRequest request, RedirectAttributes attributes) {
        {
            int flag = 0, reset = 0;
            UserInfo userInfo = (UserInfo) request.getSession().getAttribute("user_info");
            System.out.println("userInfo.getUser_code" + userInfo.getUser_code());
            String userId=userInfo.getUser_code();
            //将该用户所有default的值设置为0
            reset = addressInfoService.resetAllDefaultAddress(userId);



            int address_id = Integer.parseInt(request.getParameter("address_id"));
            //修改该地址为默认地址
            flag = addressInfoService.updateDefaultAddress(address_id);

            if (flag > 0) {
                return "redirect:qry_address_page_list";
            } else
                return "error";
        }
    }

//
//    @RequestMapping("show_edit_user")
//    public String showEditUser(HttpServletRequest request, Model model){
//        String user_code = request.getParameter("user_code");
//        //根据传入的主键让service获取UserInfo类的对象并返回给userInfo。
//        UserInfo userInfo = userInfoSerivce.getAUserInfo(user_code);
//        List<ProInfo> proInfoList = proInfoService.getProInfoList();
//        model.addAttribute("user_info", userInfo);
//        model.addAttribute("pro_list", proInfoList);
//
//        return "edit_user";//templates下面的同名网页
//    }
    //    @RequestMapping("show_edit_user")

//
//    //    @RequestMapping("edit_user")
////    public String editUser(HttpServletRequest request){
////        return "";
////    }
//    @RequestMapping("edit_user")
//    //动态绑定的方式，userInfo能自动接受网页上表单的参数名和值，并自动转换成UserInfo对象里面的值
//    public String editUser(UserInfo userInfo, RedirectAttributes attributes){
//        System.out.println("controller:" + userInfo.toString());
//        int flag = 0;
//        flag = userInfoSerivce.editUser(userInfo);
//        if (flag > 0){
//            attributes.addAttribute("field_name", "user_code");
//            attributes.addAttribute("qry_content","");
//            return "redirect:qry_user_list";
//        }
//        else
//            return "error";
//    }

    @RequestMapping("edit_address")
    //动态绑定的方式，AddressInfo能自动接受网页上表单的参数名和值，并自动转换成AddressInfo对象里面的值
    public String editAddress(HttpServletRequest request,AddressInfo addressInfo, RedirectAttributes attributes) {
        System.out.println("addressInfo: 修改地址" + addressInfo.toString());
        if (addressInfo.getDefault_address().equals("1")) {
            //将默认地址清空
            UserInfo userInfo = (UserInfo) request.getSession().getAttribute("user_info");
            System.out.println("看一下用户id" + userInfo.toString());
            String user_code = userInfo.getUser_code();
            addressInfoService.resetAllDefaultAddress(user_code);
        }
        int flag = 0;
        flag = addressInfoService.editAddress(addressInfo);
        System.out.println(flag);
        if (flag > 0) {
            return "redirect:address_page";
        } else
            return "error";
    }

    //
    @RequestMapping("qry_address_page_list")
    public String qryAddressPageList(HttpServletRequest request, Model model) {
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("user_info");
        System.out.println("看一下用户id" + userInfo.toString());
        String user_code = userInfo.getUser_code();
        int start_page = 1;// 默认起始页为第一页
        int page_size = 10;// 默认每页记录数为 10 条

        if (request.getParameter("start_page") != null && request.getParameter("page_size") != null) {
            start_page = Integer.parseInt(request.getParameter("start_page"));
            page_size = Integer.parseInt(request.getParameter("page_size"));
        }

        if (start_page<=0){start_page=1;}
        List<AddressInfo> address_list = addressInfoService.qryAddressPageList(start_page, page_size, user_code);
        model.addAttribute("address_list", address_list);
        System.out.println(address_list.toString());

        model.addAttribute("start_page", start_page);
        model.addAttribute("page_size", page_size);

        return "address/address_page";
    }


    @RequestMapping("qry_address_page_list1")
    public String qryAddressPageList1(HttpServletRequest request, Model model) {
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("user_info");
        System.out.println("看一下用户id" + userInfo.toString());
        String user_code = userInfo.getUser_code();
        int start_page = 1;// 默认起始页为第一页
        int page_size = 10;// 默认每页记录数为 10 条

        if (request.getParameter("start_page") != null && request.getParameter("page_size") != null) {
            start_page = Integer.parseInt(request.getParameter("start_page"));
            page_size = Integer.parseInt(request.getParameter("page_size"));
        }

        List<AddressInfo> address_list = addressInfoService.qryAddressPageList(start_page, page_size, user_code);
        model.addAttribute("address_list", address_list);
        System.out.println(address_list.toString());

        model.addAttribute("start_page", start_page);
        model.addAttribute("page_size", page_size);

        return "address/address_page1";
    }

//
//    @RequestMapping("show_main")
//    public String showMain(){
//        return "main";
//    }
//    @RequestMapping("form_array")
//    public String formArray(){
//        return "form_array";
//    }
//    @RequestMapping("receive_arr")
//    public String receiveArr(@RequestParam("info") String info){
//
//        return "main";
//    }

}
